<?php

namespace App\Filament\Widgets;

use App\Models\BorrowRecord;
use App\Models\Member;
use Carbon\Carbon;
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;

class MemberOverview extends BaseWidget
{
    protected static ?int $sort = 2;
    protected function getStats(): array
    {
        // 获取当前日期
        $today = Carbon::now();

        // 获取过去七天的日期范围
        $lastSevenDays = Carbon::now()->subMonths();

        // 查询最近七天每天的借阅量
        $result = Member::selectRaw('DATE(updated_at) as date, COUNT(*) as user_count')
            ->whereBetween('updated_at', [$lastSevenDays, $today])
            ->groupBy('date')
            ->orderBy('date', 'asc')
            ->pluck('user_count');
        return [
            //
            Stat::make('用户总数', Member::count())
                ->chart($result->toArray())
                ->description('总注册用户数')
                ->descriptionIcon('heroicon-o-user-plus')
                ->color('success'),
            Stat::make('今日注册', Member::whereDate('created_at',Carbon::today())->count())
                ->color('danger'),
            Stat::make('今日活跃', Member::whereDate('login_at',Carbon::today())->count())
                ->color('info'),
        ];
    }
}
